from typing import List, Optional
from sqlalchemy.orm import Session
from app.models.post import Post, PublishJob
from app.schemas.post import PostCreate, PostUpdate, PublishJobCreate


class PostCRUD:
    """帖子CRUD操作"""
    
    def create(self, db: Session, *, obj_in: PostCreate) -> Post:
        """创建帖子"""
        db_obj = Post(
            title=obj_in.title,
            content=obj_in.content,
            images=obj_in.images,
            tags=obj_in.tags,
            author_id=obj_in.author_id
        )
        db.add(db_obj)
        db.commit()
        db.refresh(db_obj)
        return db_obj
    
    def get(self, db: Session, id: int) -> Optional[Post]:
        """根据ID获取帖子"""
        return db.query(Post).filter(Post.id == id).first()
    
    def get_multi(self, db: Session, *, skip: int = 0, limit: int = 100) -> List[Post]:
        """获取帖子列表"""
        return db.query(Post).offset(skip).limit(limit).all()
    
    def get_by_status(self, db: Session, *, status: int, skip: int = 0, limit: int = 100) -> List[Post]:
        """根据状态获取帖子列表"""
        return db.query(Post).filter(Post.status == status).offset(skip).limit(limit).all()
    
    def update(self, db: Session, *, db_obj: Post, obj_in: PostUpdate) -> Post:
        """更新帖子"""
        update_data = obj_in.dict(exclude_unset=True)
        for field, value in update_data.items():
            setattr(db_obj, field, value)
        db.add(db_obj)
        db.commit()
        db.refresh(db_obj)
        return db_obj
    
    def delete(self, db: Session, *, id: int) -> Post:
        """删除帖子"""
        obj = db.query(Post).get(id)
        if obj:
            db.delete(obj)
            db.commit()
        return obj
    
    def update_status(self, db: Session, *, post_id: int, status: int, error_message: Optional[str] = None) -> Optional[Post]:
        """更新帖子状态"""
        db_obj = self.get(db, post_id)
        if db_obj:
            db_obj.status = status
            if error_message:
                db_obj.error_message = error_message
            db.add(db_obj)
            db.commit()
            db.refresh(db_obj)
        return db_obj


class PublishJobCRUD:
    """发布任务CRUD操作"""
    
    def create(self, db: Session, *, obj_in: PublishJobCreate) -> PublishJob:
        """创建发布任务"""
        db_obj = PublishJob(
            post_id=obj_in.post_id,
            job_cron=obj_in.job_cron,
            job_desc=obj_in.job_desc
        )
        db.add(db_obj)
        db.commit()
        db.refresh(db_obj)
        return db_obj
    
    def get(self, db: Session, id: int) -> Optional[PublishJob]:
        """根据ID获取发布任务"""
        return db.query(PublishJob).filter(PublishJob.id == id).first()
    
    def get_by_post_id(self, db: Session, post_id: int) -> Optional[PublishJob]:
        """根据帖子ID获取发布任务"""
        return db.query(PublishJob).filter(PublishJob.post_id == post_id).first()
    
    def get_by_xxl_job_id(self, db: Session, xxl_job_id: int) -> Optional[PublishJob]:
        """根据XXL-Job ID获取发布任务"""
        return db.query(PublishJob).filter(PublishJob.xxl_job_id == xxl_job_id).first()
    
    def get_multi(self, db: Session, *, skip: int = 0, limit: int = 100) -> List[PublishJob]:
        """获取发布任务列表"""
        return db.query(PublishJob).offset(skip).limit(limit).all()
    
    def update_xxl_job_id(self, db: Session, *, job_id: int, xxl_job_id: int) -> Optional[PublishJob]:
        """更新XXL-Job ID"""
        db_obj = self.get(db, job_id)
        if db_obj:
            db_obj.xxl_job_id = xxl_job_id
            db.add(db_obj)
            db.commit()
            db.refresh(db_obj)
        return db_obj
    
    def update_status(self, db: Session, *, job_id: int, status: int) -> Optional[PublishJob]:
        """更新任务状态"""
        db_obj = self.get(db, job_id)
        if db_obj:
            db_obj.status = status
            db.add(db_obj)
            db.commit()
            db.refresh(db_obj)
        return db_obj
    
    def delete(self, db: Session, *, id: int) -> PublishJob:
        """删除发布任务"""
        obj = db.query(PublishJob).get(id)
        if obj:
            db.delete(obj)
            db.commit()
        return obj


# 创建实例
post_crud = PostCRUD()
publish_job_crud = PublishJobCRUD()